import Config from './config'
import axios from 'axios'

const Axios = axios.create({
  baseURL: Config.baseURL,
  timeout: 30000,
  responseType: 'json',
  withCredentials: false,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }
})
Axios.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 返回状态判断(添加响应拦截器)
Axios.interceptors.response.use(
  res => {
    // 对响应数据做些事
    /* if (res.data && !res.data.success) {

     return Promise.reject(res.data.error.message)
     } */
    return res
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

export default {
  install: function (Vue, Option) {
    Object.defineProperty(Vue.prototype, '$http', {value: Axios})
  }
}
